package cn.edu.zafu.bigdata.x.zzzznnn.g;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Student {
    private String id;
    private String name;
    private String phone;
    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }
    @Override
    public String toString() {
        return "学号: " + id + ", 姓名: " + name + ", 电话: " + phone;
    }
    public String getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public String getPhone() {
        return phone;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
}
class StudentManagementSystem {
    private static final String FILE_NAME = "students.dat";
    private List<Student> students;

    public StudentManagementSystem() {
        students = new ArrayList<>();
        loadStudents();
    }
    private void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            students = (List<Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            // 如果文件不存在或读取错误，初始化空列表
            students = new ArrayList<>();
        }
    }
    private void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void addStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (isIdExists(id)) {
            System.out.println("学号已存在，请重新输入。");
            addStudent();
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        Student student = new Student(id, name, phone);
        students.add(student);
        saveStudents();
        System.out.println("学生信息添加成功。");
    }
    private boolean isIdExists(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }
    public void searchStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要检索的学号（输入QUIT退出）: ");
        String id = scanner.nextLine();

        while (!id.equals("QUIT")) {
            Student student = findStudentById(id);
            if (student!= null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生");
            }

            System.out.print("请输入要检索的学号（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }
    private Student findStudentById(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }
    public void updateStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要更新的学号（输入QUIT退出）: ");
        String id = scanner.nextLine();

        while (!id.equals("QUIT")) {
            Student student = findStudentById(id);
            if (student!= null) {
                System.out.print("请输入新的姓名: ");
                String newName = scanner.nextLine();
                student.setName(newName);
                System.out.print("请输入新的电话: ");
                String newPhone = scanner.nextLine();
                student.setPhone(newPhone);
                saveStudents();
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("未找到该学生");
            }

            System.out.print("请输入要更新的学号（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }
    public void deleteStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的学号（输入QUIT退出）: ");
        String id = scanner.nextLine();

        while (!id.equals("QUIT")) {
            Student student = findStudentById(id);
            if (student!= null) {
                students.remove(student);
                saveStudents();
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生");
            }

            System.out.print("请输入要删除的学号（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }
}
    public class main{
        public static void main(String[]args){
            StudentManagementSystem sms=new StudentManagementSystem();
            Scanner scanner=new Scanner(System.in);
                                            while(true){
                                                System.out.println("=======学生信息管理======");
                                                System.out.println("1.添加学生信息");
                                                System.out.println("2.检索学生信息");
                                                System.out.println("3.更新学生信息");
                                                System.out.println("4.删除学生信息");
                                                System.out.println("5.退出程序");
                                                System.out.println("请输入操作编号(1-5)");
                                                int choice= scanner.nextInt();
                                                scanner.nextLine();
                                                switch(choice){
                                                    case 1:
                                                        sms.addStudent();
                                                        break;
                                                    case 2:
                                                        sms.searchStudent();
                                                        break;
                                                    case 3:
                                                        sms.updateStudent();
                                                        break;
                                                    case 4:
                                                        sms.deleteStudent();
                                                        break;
                                                    case 5:
                                                        System.out.println("程序已退出，感谢使用！");
                                                        System.exit(0);
                                                    default:
                                                        System.out.println("无效选择，请重新输入。");
                                                }
                                            }
        }
}